<template>
    <view>
        <modalDialog ref="pay_main" :show.sync="pay_show" :confirm="confirm" :content="pay.tip_content"></modalDialog>
        <modalDialog ref="pay_callback" :show.sync="pay_callback.show" :confirm="payCallbackConfirm" :content="pay_callback.tip_content"
            :is_hide_cancel="true"></modalDialog>
        <modalDialog ref="command_refresh_dialog" v-if="command_refresh_dialog.show" :is_hide_cancel="true" :show.sync="command_refresh_dialog.show"
            :confirm="command_refresh_dialog.confirm" :content="command_refresh_dialog.content"></modalDialog>
    </view>
</template>

<script>
    export default {
        props: ['pay', 'appPaySuccess', 'paySuccess'],
        data() {
            return {
                pay_success: false,
                pay_callback: {
                    show: false,
                    tip_content: ''
                },
                command_refresh_dialog: {
                    show: false,
                    content: '',
                    confirm: null,
                },
            }
        },
        created() {
            if (this.pay.load_info.pay_status == this.$app.pay_status_list.PAY_STATUS_SUCCESS) {
                this._paySuccess();
            }
        },
        computed: {
            pay_show: {
                get() {
                    return this.pay.show;
                },
                set(new_value) {
                    this.pay.show = new_value;
                    this.$emit('update:pay', this.pay);
                }
            }
        },
        methods: {
            payCallbackConfirm() {
                this.pay_callback.show = false;
                if ((this.pay.load_info.pay_status == this.$app.pay_status_list.PAY_STATUS_SUCCESS ||
                        this.pay_success == true) && this.paySuccess) {
                    this.paySuccess();
                }
                this.pay_success = false;
            },
            _paySuccess(force_app) {
                this.pay_callback.show = true;
                this.pay_callback.tip_content = '支付成功';
                this.pay_success = true;

                if (this.$store.state.user.auth_token) {
                    this.$user.updateIdentityInfo(this);
                    this.$user.updateUserAssetInfo(this);
                }

                if (force_app == true) {
                    this.pay_show = false;
                    if (this.pay.formbak) {
                        this.pay.form = this.pay.formbak;
                    }
                    this.$emit('update:pay', this.pay);
                    if (this.appPaySuccess) {
                        this.appPaySuccess();
                    }
                } else {
                    // #ifdef H5
                    history.pushState(null, null, this.$nddyny.url.newUrl(null, null, [
                        'pay_status', 'pay_type'
                    ]));
                    // #endif

                    // #ifdef APP-PLUS  
                    this.pay_show = false;
                    if (this.pay.formbak) {
                        this.pay.form = this.pay.formbak;
                    }
                    this.$emit('update:pay', this.pay);
                    if (this.appPaySuccess) {
                        this.appPaySuccess();
                    }
                    // #endif
                }
            },
            confirm() {
                let service_callback_success = null;
                let service_callback_fail = (Result) => {
                    this.pay_show = false;
                    if (this.pay.form[Result.key] || this.pay.form[Result.key] == '') {
                        this.$set(this.pay.formtag, Result.key, Result.message);
                        this.$store.commit('userToastNone', '请查看页面上的错误提示')
                    } else {
                        this.command_refresh_dialog.show = true;
                        this.command_refresh_dialog.content = Result.message;
                        this.command_refresh_dialog.confirm = () => {
                            this.command_refresh_dialog.show = false;
                        }
                    }
                };
                let app_callback_success = (force_app) => {
                    this._paySuccess(force_app);
                }
                let app_callback_fail = null;
                this.$api.pay(this, this.$app.pay_type_list.PAY_TYPE_ALIPAY, this.pay.pay_uri, service_callback_success,
                    service_callback_fail, app_callback_success, app_callback_fail);
            }
        }
    }
</script>
